import React, { Component,createRef } from 'react'
import PropTypes from 'prop-types'

export default class Test1 extends Component {
  static propTypes = {
    age: PropTypes.number.isRequired
  }
  constructor(props){
    super(props)
    this.inputRef = createRef();
  }
  componentDidMount(){
    this.inputRef.current.focus()
  }
  handleAge = () => {
    if(!this.inputRef.current.value) return ;
    this.props.addAge(parseInt(this.inputRef.current.value))
    this.inputRef.current.value = null
  }

  render() {
    let { age } = this.props
    return (
      <>
        <p>age: {age}</p>
        <input type="number" ref={this.inputRef} />
        {/* <button onClick={() => this.props.addAge(+this.inputRef.current.value)}>点击增加</button> */}
        <button onClick={this.handleAge}>点击增加</button>
      </>
    )
  }
}
